package com.sxfq.loansupermarket.backsystem.modular.report.service.impl;

import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.SearchWholeHospitalInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.WholeHospitalInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class WholeHospitalsTwoNewServiceImpl implements IWholeHospitalsTwoNewService {
    @Autowired
    private IGetTotleTwoNewService getTotleTwoNewService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    IWholeHospitalnewsService service;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<WholeHospitalInfo> getTreatmentUnitList(SearchWholeHospitalInfo searchInfo) {
        searchInfo.setDimensionSelectionCustomerAll("ks");
        if ("渠道".equals(searchInfo.getDimensionSelectionTwo())) {
            return service.getOneChannelList(searchInfo);
        } else if ("年龄".equals(searchInfo.getDimensionSelectionTwo())) {
            return service.getCustomerAgeList(searchInfo);
        } else if ("媒介".equals(searchInfo.getDimensionSelectionTwo())) {
            return service.getOneMediumList(searchInfo);
        } else if ("TC".equals(searchInfo.getDimensionSelectionTwo())) {
            return service.getTcNameList(searchInfo);
        } else if ("医生".equals(searchInfo.getDimensionSelectionTwo())) {
            return service.getDoctorList(searchInfo);
        }else {
            String sql1 = "select a.id,a.department from "+redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"pay_service_grade_info",0)+" a group by department order by a.id";
            List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
            String sql = "";
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getDepartment();
                String condition = " and b.treatment_unit='" + classification + "'";
                sql += getTotleTwoNewService.getsqlDepartment(searchInfo, classification, condition, i, list1);
            }
            sql += " union all ";
            sql += getTotleTwoNewService.getTotleSqlDepartment(searchInfo);
            List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
            return list;
        }
    }

    @Override
    public List<WholeHospitalInfo> getTreatmentUnitLists(SearchWholeHospitalInfo searchInfo) {
        String condition = " and b.treatment_unit='" + searchInfo.getDimensionSelectionCustomerAll() + "'";
        if ("渠道".equals(searchInfo.getDimensionSelectionTwo())) {
            return service.getOneChannelList(searchInfo);
        } else if ("年龄".equals(searchInfo.getDimensionSelectionTwo())) {
            return service.getCustomerAgeList(searchInfo);
        } else if ("媒介".equals(searchInfo.getDimensionSelectionTwo())) {
            return service.getOneMediumList(searchInfo);
        } else if ("TC".equals(searchInfo.getDimensionSelectionTwo())) {
            return service.getTcNameList(searchInfo);
        } else if ("医生".equals(searchInfo.getDimensionSelectionTwo())) {
            return service.getDoctorList(searchInfo);
        } else {
            String sql = getTotleTwoNewService.getsqlDepartments(searchInfo, searchInfo.getDimensionSelectionCustomerAll(), condition);
            List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
            return list;
        }
    }
}